package com.wocao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wocao.entity.ResponseResult;
import com.wocao.entity.SysDictData;
import com.wocao.entity.SysDictType;
import com.wocao.entity.SysDictTypeAndDateCheck;
import com.wocao.service.SysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * 字典数据表 前端控制器
 */
@CrossOrigin
@RestController
public class SysDictDataController {

    @Autowired
    private SysDictDataService sysDictDataService;

    @Autowired
    private RedisTemplate redisTemplate;
    /*分页显示：查询*/
    @PostMapping("sysDictData-find")
    public ResponseResult<Page<SysDictData>> findSysDictData(@RequestBody SysDictTypeAndDateCheck checkData) {
        try {
            Page<SysDictData> pages = sysDictDataService.getPage(new Page<>(Long.parseLong(checkData.getCurrent()), Long.parseLong(checkData.getSize()))
                    , checkData);
            /*使用xml完成多条件查询*/
            return ResponseResult.ok(pages);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500L, "查询失败" + e.getMessage());
        }
    }
    /*分页显示*/
    @GetMapping("/sysDictData-page")
    public ResponseResult<Page<SysDictData>> getSysDictDataPages(@RequestParam(required = false, defaultValue = "1") Long current,
                                                                 @RequestParam(required = false, defaultValue = "10") Long size,
                                                                 @RequestParam("dictTypeData") String dictTypeData) {
        try {
            Page<SysDictData> pages = sysDictDataService.page(new Page<>(current, size), new QueryWrapper<SysDictData>().eq("dict_type", dictTypeData));
            return new ResponseResult<>(200L, "分页查询成功", pages);
        } catch (Exception e) {
            return new ResponseResult<>(500L, "分页失败" + e.getMessage(), null);
        }
    }

    /*删除一条*/
    @DeleteMapping("sysDictData/{id}")
    public ResponseResult<String> deleteSysDictData(@PathVariable Long id) {
        try {
            sysDictDataService.removeById(id);
            return ResponseResult.ok("ok");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500L, "删除失败" + e.getMessage());
        }
    }

    /*删除多条*/
    @PostMapping("sysDictData-some")
    public ResponseResult<String> deleteSomeSysDictData(@RequestBody List<Integer> ids) {
        try {
            sysDictDataService.removeByIds(ids);
            return ResponseResult.ok("ok");
        } catch (Exception e) {
            return ResponseResult.error(500L, "删除失败");
        }
    }

    /*添加一条数据*/
    @PostMapping("sysDictData")
    public ResponseResult<String> addSysDictData(@RequestBody SysDictData sysDictData) {
        try {
            sysDictData.setCreateTime(LocalDateTime.now());
            sysDictDataService.save(sysDictData);
            return ResponseResult.ok("ok");
        } catch (Exception e) {
            return ResponseResult.error(500L, "添加失败" + e.getMessage());
        }
    }

    /*查找一条数据*/
    @GetMapping("sysDictData-one")
    public ResponseResult<SysDictData> getSysDictDataPages(java.lang.String dictCode) {
        try {
            SysDictData sysDictData = sysDictDataService.getById(dictCode);
            return new ResponseResult<>(200L, "分页查询成功", sysDictData);
        } catch (Exception e) {
            return new ResponseResult<>(500L, "分页失败" + e.getMessage(), null);
        }
    }

    /*修改一条数据*/
    @PutMapping("sysDictData")
    public ResponseResult<String> updateSysDictData(@RequestBody SysDictData sysDictData) {
        try {
            sysDictData.setUpdateTime(LocalDateTime.now());
            sysDictDataService.updateById(sysDictData);
            return ResponseResult.ok("ok");
        } catch (Exception e) {
            return ResponseResult.error(500L, "修改失败");
        }
    }

    /*对外查询接口*/
    @GetMapping("sysDictData-find")
    @Cacheable(cacheNames = "getSysDictDataPages" ,key="T(String).valueOf(#dictType)")
    public ResponseResult<List<SysDictData>> findSysDictData(String dictType){
        try {
            List<SysDictData> sysDictDatas = sysDictDataService.selectByTree(new SysDictTypeAndDateCheck(dictType,"0"));
            /*使用xml完成多条件查询*/
            return ResponseResult.ok(sysDictDatas);
        }catch (Exception e){
            return ResponseResult.error(500L,"查询失败" + e.getMessage());
        }
    }

    /*清除缓存*/
    @PostMapping("sysDictData-clearCache")
    public ResponseResult<java.lang.String> delCache() {
        try {
            Set<String> keys = redisTemplate.keys("*");
            redisTemplate.delete(keys);
            return ResponseResult.ok("清除缓存成功！");
        } catch (Exception e) {
            return ResponseResult.error(500L, "无缓存数据可清除！");
        }
    }

    /*导出*/
    @PostMapping("sysDictData-exportNumber")
    public ResponseResult<List<SysDictType>> exportNumber(@RequestBody java.lang.String[] ids) {
        List<SysDictData> sysDictData;
        try {
            if (ids == null) {
                sysDictData = sysDictDataService.list();
            } else {
                sysDictData = sysDictDataService.listByIds(Arrays.asList(ids));
            }
            return ResponseResult.ok(sysDictData);
        } catch (Exception e) {
            return ResponseResult.error(500L, "获取导出数据失败" + e.getMessage());
        }
    }
}